﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:include value="/comm/header.jsp" />
<s:include value="/comm/menu.jsp" />
<div class="wrap1">
    	<div class="blank1"></div>
        <div class="outbox">
<!--1 start-->
			<div>
        		<div class="wrap_left">
            
                	<span class="tab7_tt">我的代购车</span>
              
            	</div>
            	<div class="wrap_right">
            		<div>
						<div class="pbox6">
							<ul>
                				<li class="bg1">&nbsp;</li>
                				<li class="s1"><span class="tt">1.&nbsp;&nbsp;确认订单信息</span></li>
                   				<li class="bg2">&nbsp;<li>
                   				<li class="s2"><span class="tt">2.&nbsp;&nbsp;托管代购款到代购岛平台</span></li>
                    			<li class="bg3">&nbsp;<li>
                    			<li class="s2"><span class="tt">3.&nbsp;&nbsp;经纪人发货</span></li>								
								<li class="bg3">&nbsp;</li>
                    			<li class="s2"><span class="tt">4.&nbsp;&nbsp;确定收货</span></li>
								<li class="bg3">&nbsp;</li>
                    			<li class="s2"><span class="tt">5.&nbsp;&nbsp;评价</span></li>
								<li class="bg4">&nbsp;</li>
                			</ul>
                        </div>
            		</div>
                    
                </div>
                <div class="clear"></div>
            </div>
            
            
<!--1 end -->

            <div class="pbox7">
               <div class="tips4"><span style="float:right" class="span10"><a href="userAddrInit.htm ">管理收货地址</a></span>确认收货地址</div>
               <ul id="userAddrs">
                   <s:iterator value="userAddrs" status="st">
               	 	<s:if test="#st.index == 0">
               	 		<li class="focus" id="mailTo<s:property value='addrId' />"><span>寄送至：</span><input name="addrRd" type="radio" value="<s:property value='addrId' />,<s:property value='userId' />, <s:property value='mailTo' />,<s:property value='person' />,<s:property value='phone' />,<s:property value='address' />,<s:property value='postcode' />" checked /> <span class="span34"> <s:property value='mailTo'/></span><span>（默认地址）</span></li>
               	 	</s:if>
               	 	<s:else>
               	 		<li id="mailTo<s:property value='addrId' />"><span></span><input name="addrRd" type="radio" value="<s:property value='addrId' />,<s:property value='userId' />, <s:property value='mailTo' />,<s:property value='person' />,<s:property value='phone' />,<s:property value='address' />,<s:property value='postcode' />" /><span><s:property value='mailTo'/></span><span class="set"><a defaultValue="<s:property value='addrId' />, <s:property value='userId' />" href="#">设置为默认收货地址</a></span></li>
               	 	</s:else>
               	 </s:iterator>
               </ul>
			</div>
<!--1 end -->
<!--  地址管理开始 -->

		<div class="pbox_address_click">
            <div class="link2"><a id="newAddress">+ 新增收货地址</a></div>
        </div>
 		<div class="pbox7"><div class="tips4">确认订单信息</div></div>   


<!--订单确认列表开始-->
	<s:form method="post" action="confirm_order" id="confirmOrder">
		<input type="hidden" name="order.broker.userid" value="<s:property value='broker.userid' />" />
		<input type="hidden" name="from" value="1" />
		<input type="hidden" name="order.address.address" id="receiveAddress" />
        <input type="hidden" name="order.address.phone" id="receivePhone" />
        <input type="hidden" name="order.address.postcode" id="receivePostcode" />
		<input type="hidden" name="order.address.person" id="receivePerson" />
		<input type="hidden" name="order.logisticCharge" value="<s:property value='logisticCharge' />" />
        <div class="pbox12">
				
                <ul>
                	<li class="t1 tt">&nbsp;</li>
                    <li class="t2 tt"><span>商品</span></li>
                    <li class="t3 tt"><span>价格</span></li>
                    <li class="t4 tt"><span>服务费</span></li>
                    <li class="t5 tt"><span>数量</span></li>
                    <li class="t6 tt"><span>小计</span></li>
					<div class="clear"></div>
                    
                </ul>
					<div class="t8"><strong>经纪人：</strong><a href="brokerDetail.htm?id=<s:property value='broker.userid' />" target="_blank"><s:property value="broker.nickName" /></a></div>		
					<s:iterator value="items">
							<input type="hidden" name="productIds" value="<s:property value='productId' />" />
							<ul class="ttbox">
                				<li class="t1"><div class="pbox1_list_pic"><div><p><img src="<s:property value='absoluteImage' />" /></p></div></div></li>
                    			<li class="t2 t2_a"><span>&nbsp;&nbsp;<s:property value="productName" /></span></li>
                    			<li class="t3"><span class="span18">￥<s:property value="price" /></span></li>
                    			<li class="t4"><span class="span19">￥<s:property value="serviceFee" /></span></li>
                    			<li class="t5"><span><s:property value="number" /></span></li>
                    			<li class="t6"><span class="span18">￥<s:property value="subtotal" /></span></li>
                    			<div class="clear"></div>
                			</ul>
					</s:iterator>
					<div class="blank1"></div>
					<div class="pbox12">
            			<div class="wrap_left cbox1">
            				给经纪人留言：
            				<input name="order.message" type="text" class="input1" size="40"/>
            			</div>
               			<div class="wrap_right cbox1">
               				运费：<span class="span2" id="shippingCost">￥<s:property value="logisticCharge" /></span></div>
                		<div class="clear"></div>
        			</div>
			</div>
</s:form>
 <!--订单确认列表结束-->  
 		
		<div class="at0">
			<div class="at1">合计：<span class="span2" id="total">￥<s:property value='totalPrice' /></span><br />寄送至：<br /><span class="span34" id="mailTo"><s:if test="defaultAddr != null"><s:property value='defaultAddr.mailTo' /></s:if></span></div>
			<div class="clear"></div>
			<div class="at2">
				<span class="but_sub2"><a href="cart.htm">返回代购车</a></span>&nbsp;&nbsp;&nbsp;&nbsp;
				<span  class="but_sub1"><a href="javascript:confirmOrder()">提交订单</a></span></div>
		</div>
		
        
	
        
        </div>
</div>
<s:include value="/comm/bottom.jsp"/>

<script type="text/javascript">
var lastCheckId = 0;
var lastDefaultId = 0;
var userId = "<s:property value='#session.user.userid' />";
var isLoad = false;
$(window).load(function(){
	var backUrl = '${requestScope.backUrl}';
	
	if ($("[name=addrRd]").length == 0) {
		$("#cancel").html("<a href='" + backUrl + "'>返回上一步</a>");
		$("#closeAddress").attr("href", backUrl);
		showDiv();
	}
	else {
		lastCheckId =  $("[name=addrRd]:checked").val().split(",")[0];
		lastDefaultId = lastCheckId;
		load();
		isLoad = true;
	}
});

	function load() {
		$("#logisticsId").change(function() {
			var value = $(this).val();
			var array = value.split(",");
			selectLogistics(array[0], array[1]);
		});
		
		$("[name=addrRd]").live("click", function() {
			selectAddress($(this).val());
		});
		$("#userAddrs a").live("click", function() {
			setDefault($(this).attr("defaultValue"));
		});

		$("#newAddress").click(function() {
			$("#cancel").html("<a href='javascript:closeDiv();'>取消</a>");
			showDiv();
		});
	}
	
	function selectLogistics(id, price) {
		$("#shippingCost").html("￥" + price);
		var lastPrice = $("#logisticsPrice").val();
		$("#logisticsPrice").val(price);
		var totalPrice = parseFloat($("#total").html().substring(1)) + parseFloat(price) - lastPrice;
		$("#total").html("￥" + totalPrice.toFixed(2));
		
	}

	function selectAddress(curCheckValue) {
		var array = curCheckValue.split(",");
		var curCheckId = array[0];
		$("#mailTo" + lastCheckId).removeClass("focus");
		$("#mailTo" + lastCheckId + " span:eq(0)").html("");
		$("#mailTo" + lastCheckId + " span:eq(1)").removeClass("span34");

		$("#mailTo" + curCheckId).addClass("focus");
		$("#mailTo" + curCheckId + " span:eq(0)").html("寄送至：");
		$("#mailTo" + curCheckId + " span:eq(1)").addClass("span34");
		
		lastCheckId = curCheckId;
		
		$("#mailTo").html(array[2]);
	}


	function setDefault(curDefaultValue) {
		var array = curDefaultValue.split(",");
		$.ajax({
				url:'setDefaultAddrForAjax.htm',
				type:'post',
				data:{"userAddr.addrId":array[0],"userAddr.userId":array[1]},
				success:function(json) {
					if (json.ajaxSuccess) {
						$("#mailTo" + lastDefaultId + " span:eq(2)").addClass("set");
						$("#mailTo" + lastDefaultId + " span:eq(2)").html("<a defaultValue='" + lastDefaultId + "," + array[1] + "' href='javascript:void(0)'>设置为默认收货地址</a>");
						$("#mailTo" + array[0] + " span:eq(2)").removeClass("set");
						$("#mailTo" + array[0] + " span:eq(2)").html("（默认地址）");
						lastDefaultId = array[0];
					}
				}
				
			}
		);
	}
	
	function save() {
		var defaultAddr = 0;
		if ($("#defaultAddr").attr("checked")) {
			defaultAddr = $("#defaultAddr").val();
		}
		//清空操作
		$("#person_msg").html("");
		$("#province_msg").html("");
		$("#address_msg").html("");
		$("#postcode_msg").html("");
		$("#phone_msg").html("");
		
		var person = $.trim($("#person").val());
		var province = $.trim($("#s1").val());
		var city = $.trim($("#s2").val());
		var street = $.trim($("#address").val());
		var postcode = $.trim($("#postcode").val());
		var phone = $.trim($("#phone").val());
		
		if(person==''){
			$("#person_msg").html("收货人姓名不能为空!");
			return;
		}
		
		if(person.length>20){
   			$("#person_msg").html("收货人姓名 不能超过20个字符!");
   			return;
   		}
		
		if(province=='请选择' || city == '请选择'){
			$("#province_msg").html("所在地区未选择!");
   			return;
		}
		
		if(street==''){
			$("#address_msg").html("街道地址不能为空!");
			return;
		}
		
		if(street.length>180){
   			$("#address_msg").html("街道地址 不能超过180个字符!");
   			return;
   		}
		
		if(!(postcode=='')){
			var reg =/^[0-9]{6}$/;
			//必须为6位数字
			if(!reg.test(postcode)){
				$("#postcode_msg").html("邮政编码格式错误!");
	   			return;
			}
		}
		
		if(phone==''){
			$("#phone_msg").html("联系电话不能为空!");
			return;
		}
		
		if(!(phone=='')){
			var reg1 =/^0{0,1}(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$/;
			var reg2 = new RegExp("^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$");
			
		  	if(!reg1.test(phone) && !reg2.test(phone)){
			  	$("#phone_msg").html("联系电话格式不正确!");
			  	return;
		  	}
		}
		$.ajax({
			url:'saveAddrForAjax.htm',
			type:'post',
			data:{"userAddr.person":person,"userAddr.street":street,"userAddr.phone":phone,"userAddr.postcode":postcode,"userAddr.province":province,"userAddr.city":city,"userAddr.defaultAddr":defaultAddr,"userAddr.userId":userId},
			success:function(json) {
				closeDiv();
				if (json.ajaxMsg != null) {
					alert(json.ajaxMsg);
				}
				else {
					refreshAddress(json.userAddr);
				}
			}
		});
		closeDiv();
	}

	function confirmOrder() {
		var values = $("[name=addrRd]:checked").val().split(",");
		$("#receivePerson").val(values[3]);
		$("#receivePhone").val(values[4]);
		$("#receiveAddress").val(values[5]);
		$("#receivePostcode").val(values[6]);
		$("#confirmOrder").submit();
	}

	function refreshAddress(userAddr) {
		var li = "<li id='mailTo" + userAddr.addrId + "' class='focus'><span>寄送至：</span>";
		var value = userAddr.addrId + "," + userAddr.userId + "," + userAddr.mailTo + "," + userAddr.person + "," + userAddr.phone + "," + userAddr.address + "," + userAddr.postcode;
		li += "<input name='addrRd' type='radio' value='" + value + "' checked />";
		li += "<span class='span34'>" + userAddr.mailTo + "</span>";
		if (userAddr.defaultAddr == 1) {
			li += "<span>（默认地址）</span>";
			if (lastDefaultId != 0) {
				$("#mailTo" + lastDefaultId + " span:eq(2)").addClass("set");
				$("#mailTo" + lastDefaultId + " span:eq(2)").html("<a defaultValue='" + lastDefaultId + "," + userAddr.userId  + "' href='javascript:void(0);'>设置为默认收货地址</a>");	
			}
			lastDefaultId = userAddr.addrId;
		}
		else {
			li += "<span class='set'><a defaultValue='" + userAddr.addrId + "," + userAddr.userId + "' href='javascript:void(0);'>设置为默认收货地址</a></span>"; 
		}
		li += "</li>";
		$("#userAddrs").append(li);
		if (lastCheckId != 0) {
			$("#mailTo" + lastCheckId + " input").attr("checked", false);
			$("#mailTo" + lastCheckId).removeClass("focus");
			$("#mailTo" + lastCheckId + " span:eq(0)").html("");
			$("#mailTo" + lastCheckId + " span:eq(1)").removeClass("span34");	
		}
		lastCheckId = userAddr.addrId;
		$("#mailTo").html(userAddr.mailTo);
		
		if (!isLoad) {
			load();
			isLoad = true;
		}
	}
</script>

<script type="text/javascript">
function showDiv(){
var arrayPageSize   = getPageSize();//调用getPageSize()函数
var arrayPageScroll = getPageScroll();//调用getPageScroll()函数
document.getElementById('pbox_address').style.display='block';
document.getElementById('pbox_address').style.top = arrayPageScroll[1] + (arrayPageSize[3] - 35 - 400) / 2 + 'px';
document.getElementById('pbox_address').style.left = (arrayPageSize[0] - 20 - 750) / 2 + 'px';
document.getElementById('fixbg').style.display='block';
}


function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		sScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	// var pageHeight,pageWidth;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement
			&& document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	var pageWidth, pageHeight
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
	return arrayPageSize;
}
// 获取滚动条的高度
function getPageScroll() {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('', yScroll)
	return arrayPageScroll;
}

function closeDiv(){
document.getElementById('pbox_address').style.display='none';
document.getElementById('fixbg').style.display='none';
}
</script>
<s:include value="address.jsp"></s:include>